<?php
function batch_delete_node_type_confirm(&$form_state){
	$form = array();
	$types = node_get_types();
	$names = node_get_types('names');
	$prev = array(
	  '#type' => 'button',
	  '#attributes' => array('onClick' => 'location.href = "'. url('admin/content/types/batch_delete') . '"; return false;'),
		'#value' => t('Go to previous page'),
	);

	$delete = &$form_state['post']['delete'];
	if (!isset($delete) || 0 == count($delete)) {
	  drupal_set_message(t('Must select least one content type.'), 'error');
		$form['prev'] = $prev;
		return $form;
	}

	$list = array();
	$options = array();
	$status = array();
	foreach ($delete as $item){
	  if (isset($names[$item])) {
			$type = $types[$item];
			if ($type->custom){
			  $list[$item] = $names[$item] . ' (' . $item . ')';
			  $options[$item] = $names[$item];
			  $status[] = $item;
			} else {
				drupal_set_message(t('The content type %name can not be deleted.', array('%name' => $list[$item])), 'error');
			}
		} else {
			drupal_set_message(t('The content type %name is not exist.', array('%name' => $list[$item])), 'error');
		}
	}

	$form['delete'] = array(
		'#type' => 'checkboxes',
		'#options' => $options,
		'#default_value' => $status,
		'#process' => array('expand_checkboxes', 'batch_delete_deiable_checkbox'),
	);

  $message = t('Delete content type');
  $caption = t('Are you sure you want to delete the content type %type?', array('%type' => implode(', ', $list)));
  return confirm_form($form, $message, 'admin/content/types/batch_delete', $caption, t('Delete'));
}

function batch_delete_node_type_confirm_submit($form, &$form_state) {
	$names = node_get_types('names');
	$delete = &$form_state['values']['delete'];
	foreach($delete as $item){
	  $name = $names[$item] . ' (' . $item . ')';
    node_type_delete($item);
    $t_args = array('%name' => $name);
    drupal_set_message(t('The content type %name has been deleted.', $t_args));
    watchdog('menu', 'Deleted content type %name.', $t_args, WATCHDOG_NOTICE);
	}

  node_types_rebuild();
  menu_rebuild();
  $form_state['redirect'] = 'admin/content/types/batch_delete';
  return;
}

function batch_delete_deiable_checkbox(&$element){
	foreach ($element['#options'] as $key => $value){
	  $element[$key]['#attributes']['disabled'] = 'disabled';
	}
	return $element;
}
